package com.itheima.demo;

import com.itheima.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

public class Demo1 {
    public static void main(String[] args) throws Exception {
        //1、读取mybatis核心配置文件的信息:运行环境，映射配置文件信息
        InputStream is = Resources.getResourceAsStream("sql.xml");
        //2、构建SqlSessionFactory对象(构建者模式)
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //3、获取SqlSession对象
        SqlSession session = factory.openSession();
        //4、调用具体的statement对象，执行sql
        List<User> list = session.selectList("User.selectAll");
        //处理结果
        for (User user : list) {
            System.out.println(user);
        }
        //5、事务提交(查询事务用事务，没事务就算了)
        session.commit();
        //6、释放资源
        session.close();
        is.close();
    }
}
